package com.echo.im.client.handler.suport;


import com.echo.im.client.handler.MessageHandler;
import com.echo.im.client.status.OnlineStatusService;
import com.echo.im.common.enums.IMCmdType;
import com.echo.im.common.enums.IMTerminalType;
import com.echo.im.common.model.info.IMRecvInfo;
import com.echo.im.common.model.info.IMUserInfo;
import com.echo.im.common.model.message.IMGroupMessage;
import com.echo.im.common.vo.message.GroupMessageVO;

import com.echo.im.infrastructure.mq.event.model.push.PushMessageEvent;
import com.echo.im.infrastructure.mq.event.producer.EventPublisher;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: echo
 * @time: 2024-08-14
 */
@Component
@RequiredArgsConstructor
public class GroupMessageHandler implements MessageHandler<IMGroupMessage> {

    private final EventPublisher eventPublisher;
    private final PushMessageEvent pushMessageEvent;
    private final OnlineStatusService onlineStatusService;

    @Override
    public void handleMessage(IMGroupMessage message) {
        // 根据群聊每个成员所连的IM-engin，进行分组
        Map<Integer, List<IMUserInfo>> serverIdUserInfoMap = onlineStatusService.getServerIdUserInfoListMap(message.getRecvIds());

        // 逐个server发送
        serverIdUserInfoMap.forEach(
                (serverId, userInfoList) -> {
                    IMRecvInfo<GroupMessageVO> recvInfo = new IMRecvInfo<>();
                    recvInfo.setCmd(IMCmdType.GROUP_MESSAGE.code());
                    recvInfo.setSender(message.getSender());
                    recvInfo.setReceivers(new LinkedList<>(userInfoList));
                    recvInfo.setData(message.getData());
                    // 推送至队列
                    eventPublisher.publishWithServerId(pushMessageEvent, recvInfo,serverId.toString());
                }
        );

        // 对于离线用户 群聊消息不负责推送，由用户自己拉取

        // 推送给自己的其他终端
        if (message.getSendToSelf()) {
            List<Integer> recvTerminals= IMTerminalType.codes().stream().filter(terminal -> !terminal.equals(message.getSender().getTerminal())).collect(Collectors.toList());
            Map<Integer, List<Integer>> serverIdMap = onlineStatusService.getServerIdTerminalListMap(message.getSender().getId(), recvTerminals);
            serverIdMap.forEach((serverId, terminalList)->{
                //组装消息
                IMRecvInfo<GroupMessageVO> recvInfo = new IMRecvInfo<>();
                recvInfo.setCmd(IMCmdType.GROUP_MESSAGE.code());
                recvInfo.setSender(message.getSender());
                List<IMUserInfo> userInfos = new ArrayList<>();
                for (Integer terminal : terminalList) {
                    userInfos.add(new IMUserInfo(message.getSender().getId(), terminal));
                }
                recvInfo.setReceivers(userInfos);
                recvInfo.setData(message.getData());

                // 如果终端在线，将数据推送到消息队列，等待拉取推送
                eventPublisher.publishWithServerId(pushMessageEvent, recvInfo,serverId.toString());
            });
        }
    }
}
